
using System.ServiceProcess;
using TripleA.Core.Interfaces;
using TripleA.Core.Interfaces.Entities;

namespace TripleA.Core.Extensions.WindowsService
{
    public static class WindowsSeviceExtensions
    {
        public static IPipelineConfig<T> ServiceIsRunningAsIdentity<T>(this IPipelineConfig<T> config,            
            string serviceName,
            string identity)
            where T : Context
        {
            return ServiceIsRunningAsIdentity(config, null, serviceName, identity);
        }

        public static IPipelineConfig<T> ServiceIsRunningAsIdentity<T>(this IPipelineConfig<T> config,
            string machine,
            string serviceName,
            string identity)
            where T : Context
        {
            config.RunStep(new WindowsServiceRunningAsStep<T>
                               {
                                   Machine = machine,
                                   ServiceName = serviceName,
                                   Identity = identity
                               });
            return config;
        }

        public static IPipelineConfig<T> ServiceInstalled<T>(this IPipelineConfig<T> config,            
            string serviceName)
            where T : Context
        {
            return ServiceInstalled(config, null, serviceName);
        }

        public static IPipelineConfig<T> ServiceInstalled<T>(this IPipelineConfig<T> config,
            string machine,
            string serviceName)
            where T : Context
        {
            config.RunStep(new WindowsServiceInstalledStep<T>
                               {
                                   Machine = machine,
                                   ServiceName = serviceName
                               });
            return config;
        }

        public static IPipelineConfig<T> ServiceStartMode<T>(this IPipelineConfig<T> config,
                                                             string serviceName,
                                                             ServiceStartMode startMode)
            where T : Context
        {
            return ServiceStartMode(config, null, serviceName, startMode);
        }

        public static IPipelineConfig<T> ServiceStartMode<T>(this IPipelineConfig<T> config,
            string machine,
            string serviceName,
            ServiceStartMode startMode)
            where T : Context
        {
            config.RunStep(new WindowsServiceStartModeStep<T>
                               {
                                   Machine = machine,
                                   ServiceName = serviceName,
                                   StartMode = startMode
                               });
            return config;
        }

        public static IPipelineConfig<T> ServiceStatus<T>(this IPipelineConfig<T> config,
            string serviceName,
            ServiceControllerStatus status)
            where T : Context
        {
            return ServiceStatus(config, null, serviceName, status);
        }

        public static IPipelineConfig<T> ServiceStatus<T>(this IPipelineConfig<T> config,
            string machine,
            string serviceName,
            ServiceControllerStatus status)
            where T : Context
        {
            config.RunStep(new WindowsServiceStatusStep<T>
                               {
                                   Machine = machine,
                                   ServiceName = serviceName,
                                   Status = status
                               });
            return config;
        }

        public static IPipelineConfig<T> StartService<T>(this IPipelineConfig<T> config,
            string serviceName)
            where T : Context
        {
            return StartService(config, null, serviceName);
        }

        public static IPipelineConfig<T> StartService<T>(this IPipelineConfig<T> config,
            string machine,
            string serviceName)
            where T : Context
        {
            config.RunStep(new WindowsServiceControlStep<T>
            {
                Machine = machine,
                ServiceName = serviceName,
                Action = WindowsServiceActionTypes.Start
            });
            return config;
        }

        public static IPipelineConfig<T> StopService<T>(this IPipelineConfig<T> config,
            string serviceName)
            where T : Context
        {
            return StopService(config, null, serviceName);
        }

        public static IPipelineConfig<T> StopService<T>(this IPipelineConfig<T> config,
            string machine,
            string serviceName)
            where T : Context
        {
            config.RunStep(new WindowsServiceControlStep<T>
            {
                Machine = machine,
                ServiceName = serviceName,
                Action = WindowsServiceActionTypes.Stop
            });
            return config;
        }

    }
}